package com.tz.yiqingapp.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tz.yiqingapp.bean.*;
import com.tz.yiqingapp.config.AccessLimit;
import com.tz.yiqingapp.dto.PageDTO;
import com.tz.yiqingapp.mapper.*;
import com.tz.yiqingapp.mapper.UserMapper;
import com.tz.yiqingapp.utils.MyPasswordEncoder;
import com.tz.yiqingapp.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin")
public class AdminUser {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserMapper studentMapper;
    @Autowired
    UserHealthMapper userHealthMapper;
    @Autowired
    UserHealthReportMapper userHealthReportMapper;
    @Autowired
    YimiaoOrderMapper yimiaoOrderMapper;
    @Autowired
    HosptialMapper hosptialMapper;

    @AccessLimit(times = 5)
    @PostMapping("/searchUser")
    // @ApiOperation(value = "查询User", notes = "查询User名称", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> searchUser(@RequestBody User user, HttpServletRequest request) {

        if(request.getSession().getAttribute("loginrole") == null ||
            !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.or().lambda().like(User::getName,user.getName());
        List<User> userList = userMapper.selectList(userQueryWrapper);
        if(userList.size() >0) {
            return ResponseUtils.success(userList);
        }
        return ResponseUtils.fail("user not exits");
    }

    @AccessLimit(times = 5)
    @PostMapping("/getUserList")
    @ResponseBody
    public HashMap<String, Object> getUserList(@RequestBody PageDTO pageDTO, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().ne(User::getStatus,1);
        Page<User> userPage = new Page<>(pageDTO.getCurrentpage() , pageDTO.getPagesize());
        IPage<User> userIPage = userMapper.selectPage(userPage , userQueryWrapper);
        List<User> users = userIPage.getRecords();
        List<UserBaseInfo> userInfos = new ArrayList<>();
        for (User u : users) {
            UserBaseInfo userInfo = new UserBaseInfo(u);

            QueryWrapper<UserHealth> userHealthQueryWrapper = new QueryWrapper<>();
            userHealthQueryWrapper.lambda().eq(UserHealth::getUid,u.getId());
            List<UserHealth> userHealths = userHealthMapper.selectList(userHealthQueryWrapper);
            if(userHealths.size()>0)
                userInfo.setUserHealth(userHealths.get(0));

            QueryWrapper<UserHealthReport> userHealthReportQueryWrapper = new QueryWrapper<>();
            userHealthReportQueryWrapper.lambda().eq(UserHealthReport::getUid,u.getId());
            List<UserHealthReport> userHealthReportList = userHealthReportMapper.selectList(userHealthReportQueryWrapper);
            if(userHealthReportList.size()>0)
                userInfo.setUserHealthReport(userHealthReportList.get(0));

            QueryWrapper<YimiaoOrder> yimiaoOrderQueryWrapper = new QueryWrapper<>();
            yimiaoOrderQueryWrapper.lambda().eq(YimiaoOrder::getUid,u.getId());
            List<YimiaoOrder> yimiaoOrders = yimiaoOrderMapper.selectList(yimiaoOrderQueryWrapper);
            if(yimiaoOrders.size()>0){
                YimiaoOrderInfo yimiaoOrderInfo = new YimiaoOrderInfo(yimiaoOrders.get(0));
                Hosptial hosptial = hosptialMapper.selectById(yimiaoOrders.get(0).getHid());
                yimiaoOrderInfo.setHosptial(hosptial);
                userInfo.setYimiaoOrderInfo(yimiaoOrderInfo);
            }
            userInfos.add(userInfo);
        }
        return ResponseUtils.successlist(  userIPage.getTotal(),userIPage.getPages(),userIPage.getCurrent(),userInfos);
    }

    @AccessLimit(times = 5)
    @PostMapping("/getAllUser")
    @ResponseBody
    public HashMap<String, Object> getAllUser(@RequestBody PageDTO pageDTO, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().ne(User::getStatus,1);
        List<User> users = userMapper.selectList(userQueryWrapper);
        return ResponseUtils.success(users);
    }
    @AccessLimit(times = 5)
    @PostMapping("/addUser")
    // @ApiOperation(value = "添加User", notes = "软删除User参数", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> addUser(@RequestBody User user, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.or().lambda().eq(User::getName,user.getName());
        List<User> userList = userMapper.selectList(userQueryWrapper);
        if(userList.size() >0) {
            return ResponseUtils.fail("user exits");
        }
        user.setPassword(MyPasswordEncoder.doencode(user.getPassword()));
        userMapper.insert(user);
        return ResponseUtils.success("user exits");
    }


    @AccessLimit(times = 5)
    @PostMapping("/updateUser")
    // @ApiOperation(value = "修改User信息", notes = "修改User信息", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> updateUser(@RequestBody User user, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        if(user.getPassword() != null)
            user.setPassword(MyPasswordEncoder.doencode(user.getPassword()));
        userMapper.updateById(user);
        return ResponseUtils.success(user);
    }

    @AccessLimit(times = 5)
    @PostMapping("/delUser")
    // @ApiOperation(value = "软删除Userid", notes = "软删除User", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> delUser(@RequestBody User deluser, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        User user = userMapper.selectById(deluser.getId());
        if(user != null) {
            user.setStatus(1);
            return ResponseUtils.success("user delete success");
        }
        return ResponseUtils.fail("user delete fail");

    }
}
